<template>
  <div>
    <div class="row-header">
      <el-form :model="form" ref="form" :rules="formRules" :inline="true" label-width="150px">
        <el-form-item label="流量池名称" prop="flowPoolName">
          <el-input size="small" style="width: 100%;" v-model="form.flowPoolName"></el-input>
        </el-form-item>
        <el-form-item label="运营商" prop="dictOperator">
          <el-select size="small" placeholder="请选择" style="width: 95%;" v-model="form.dictOperator">
            <el-option v-for="dictOperator in form.dictOperatorSelect" :key="dictOperator.id" :label="dictOperator.name"
                       :value="dictOperator.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="row-form-btn">
        <el-button type="shenda" icon="el-icon-search" size="small" @click="currentPage = 1 ; initData()">查询</el-button>
      </div>
    </div>
    <div class="split-line"></div>
    <div class="row-main">
      <el-button type="shenda" @click="toBack" size="mini" icon="el-icon-back">返回</el-button>
      <el-button  size="mini" icon="el-icon-receiving" @click="showTipPanel">批量设置权限数</el-button>
      <el-table :header-cell-style="{ 'background':$store.state.app.tableBgColor,'color':'#606266' }" :data="tableData" tooltip-effect="dark"
                class="row-main-table" @selection-change="handleSelectionChange" v-loading.fullscreen="tableLoading"
                element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.2)" :row-key="getRowKeys">
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column prop="flowPoolName" label="流量池名称" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="toUsage">{{ scope.row.flowPoolName }}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="firstpoolName" label="所属一级流量池" align="center"></el-table-column>
        <el-table-column prop="dictOperator" label="所属运营商" align="center"></el-table-column>
        <el-table-column prop="theoryFlow" label="理论流量" align="center"></el-table-column>
        <el-table-column prop="containsFlow" label="实际流量" align="center"></el-table-column>
        <el-table-column prop="usedFlow" label="实际已使用" align="center"></el-table-column>
        <el-table-column prop="remainingFlow" label="剩余实际流量" align="center"></el-table-column>
        <el-table-column prop="cardNumber" label="挂卡数量" align="center"></el-table-column>
        <el-table-column prop="activationCardNumber" label="已开卡数" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="toCardList">{{ scope.row.activationCardNumber }}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="unopenedCardNumber" label="未开卡数" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="toCardList">{{ scope.row.unopenedCardNumber }}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="cao" label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="toUsage(scope.row)">查看用量统计</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div :style="{'overflow-x': $store.state.app.screenWidth<992?'scroll':'none'}">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" class="row-main-pager"
                     :current-page.sync="currentPage" :page-size="pageSize" layout="total,sizes,prev, pager, next, jumper"
                     :page-sizes="[10,15,50,100]" background :total="total"></el-pagination>
      </div>
    </div>
    <el-dialog class="delete" top="33vh" :visible.sync="isSettingImaginaryNumber" :append-to-body="true" width="30%">
      <el-form :model="a" :rules="b" ref="form1">
        <el-form-item label="设置权限数" label-width="80px" prop="cardFlowImaginary">
          <el-input size="small" v-model="a.cardFlowImaginary"></el-input>
        </el-form-item>
      </el-form>
      <span class="dialog-footer" slot="footer">
      <el-button @click="isSettingImaginaryNumber = false" size="small">取消</el-button>
      <el-button type="primary" size="small" @click="showSecondaryPromptPanel('form1')">确定</el-button></span>
    </el-dialog>
    <el-dialog class="delete" top="33vh" :visible.sync="isCovers" :append-to-body="true" width="30%"><span>&nbsp;&nbsp;&nbsp;&nbsp;业务号码为 :</span><span
      style="display: flex; justify-content: center; color: red;">{{ this.cardNumber }}</span><span
      style="display: flex; justify-content: center;">&nbsp;&nbsp;的卡已经设置过权限数，是否重新设置权限数？</span><span
      class="dialog-footer" slot="footer">
      <el-button @click="submitSkipBatchSetImaginaryNumberHandle" size="small">跳过覆盖</el-button>
      <el-button type="primary" size="small" @click="submitForceBatchSetImaginaryNumberHandle">强制覆盖</el-button></span>
    </el-dialog>
  </div>

</template>

<script>
import {listForAgent, batchSettingImaginaryNumber, checkBatchSettingImaginaryNumber} from '@/api/getData'

export default {
  name: 'Next-Level',
  data() {
    const validateFlowPoolName = (rule, value, callback) => {
      if (value.length > 50) {
        callback(new Error('流量池名称不能超过50位'))
      } else {
        callback()
      }
    }
    return {
      maxPage: 1,
      tableData: [],
      pageSize: 10,
      currentPage: 1,
      total: 0,
      ascs: [],
      descs: [],
      value: false,
      tableLoading: false,
      isSettingImaginaryNumber: false,
      isCovers: false,
      form: {
        flowPoolName: '',
        dictOperator: '',
        dictOperatorSelect: []
      },
      a: {cardFlowImaginary: ''},
      b: {
        cardFlowImaginary: [
          {required: true, message: '请输入权限数', trigger: 'blur'},
          {
            pattern: /^(([1-9][0-9]*)|(([0]\.\d{0,1}|[1-9][0-9]*\.\d{0,1})))$/,
            message: '请输入数字,精确到小数点后一位',
            trigger: 'blur'
          }
        ]
      },
      flowPoolName: '',
      dictOperator: '',
      theoryFlow: '',
      containsFlow: '',
      usedFlow: '',
      remainingFlow: '',
      cardNumber: '',
      activationCardNumber: '',
      unopenedCardNumber: '',
      multipleSelection: [],
      formRules: {
        flowPoolName: [
          {message: '请填写流量池名称', trigger: 'blur'},
          {validator: validateFlowPoolName}
        ]
      },
      firstPoolId: ''
    }
  },
  created() {
    this.initData()
  },
  methods: {
    getRowKeys(row) {
      return row.id
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.initData()
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initData()
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    async initData() {
      const returnData = await listForAgent({
          firstPoolId: this.firstPoolId,
          flowPoolName: this.form.flowPoolName,
          page: {
            current: this.currentPage + '',
            size: this.pageSize + '',
            ascs: this.ascs,
            descs: this.descs
          }
        },
        {
          obj: this,
          key: 'tableLoading'
        })
      this.tableData = [];
      this.total = 0;
      if (returnData._result) {
        this.maxPage = Math.ceil(returnData.data.total / this.pageSize) // 分页的最大页数
        // 数据回显
        returnData.data.records.map(item => {
          this.form.dictOperatorSelect.push({id: item.id, name: item.dictOperator})
          const obj = {}
          obj.id = item.id
          obj.flowPoolName = item.flowPoolName // 流量池名称
          obj.firstpoolName = item.firstpoolName // 流量池名称
          obj.dictOperator = item.dictOperatorName // 所属运营商
          obj.theoryFlow = item.theoryFlow // 理论流量
          obj.containsFlow = item.containsFlow // 实际流量
          obj.usedFlow = item.usedFlow // 实际已使用
          obj.remainingFlow = item.remainingFlow // 剩余实际流量
          obj.cardNumber = item.cardNumber // 挂卡数量
          obj.activationCardNumber = item.activationCardNumber // 已开卡数
          obj.unopenedCardNumber = item.unopenedCardNumber // 未开卡数
          this.tableData.push(obj)
        })
        this.tableLoading = false;
        this.total = returnData.data.total;
      }
    },
    toBack() {
      this.$router.back(-1);
    },
    toUsage() {
      this.$router.push({path: '/package/usage'})
    },
    toCardList() {
      this.$router.push({path: '/package/next-card-list'})
    },
    // 显示提示面板
    showTipPanel() {
      if (this.multipleSelection.length !== 0) {
        this.isSettingImaginaryNumber = true
      } else {
        this.$message({message: '请选择要设置虚数的卡组', type: 'error'})
      }
    },
    // 校验
    showSecondaryPromptPanel(form1) {
      this.$refs[form1].validate(async valid => {
        if (valid) {
          const returnData = await checkBatchSettingImaginaryNumber({
            ids: this.multipleSelection.map(item => item.id).join(','),
            cardFlowImaginary: this.a.cardFlowImaginary
          })
          if (returnData._result) {
            if (returnData.data.length !== 0) {
              this.isCovers = true
              this.cardNumber = returnData.data.map(item => item.cardNumber).join(',')
            } else {
              this.submitSkipBatchSetImaginaryNumberHandle()
            }
          }
        }
      })
    },
    // 跳过批量设置虚数
    async submitSkipBatchSetImaginaryNumberHandle() {

      const returnData = await batchSettingImaginaryNumber({
        ids: this.multipleSelection.map(item => item.id).join(','),
        cardFlowImaginary: this.a.cardFlowImaginary,
        isCover: 'NOCOVER'
      })
      if (returnData._result) {
        this.isCovers = false
        this.isSettingImaginaryNumber = false
        this.$message({message: '批量设置虚数成功', type: 'success'})
      }
    },
    // 强制覆盖
    async submitForceBatchSetImaginaryNumberHandle() {
      const returnData = await batchSettingImaginaryNumber({
        ids: this.multipleSelection.map(item => item.id).join(','),
        cardFlowImaginary: this.a.cardFlowImaginary,
        isCover: 'COVER'
      })
      if (returnData._result) {
        this.isCovers = false
        this.isSettingImaginaryNumber = false
        this.$message({message: '批量设置虚数成功', type: 'success'})
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>
